package com.cxhd.pacs;

import com.cxhd.pacs.external.IntefaceOther;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jfinal.kit.JsonKit;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jasig.cas.client.authentication.AttributePrincipal;
//import org.springframework.util.StringUtils;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.TypeReference;
//import com.cxhd.guahao.config.DefaultConfig;
//import com.cxhd.guahao.model.User;
//import com.cxhd.guahao.service.UserService;
//import com.cxhd.guahao.util.ApplicationContextAwareUtil;

public class LoginFilter implements Filter {
	
	//用于获取用户的信息
	//private UserService userService = null;
	
	@Override
	public void doFilter(ServletRequest sRequest, ServletResponse sResponse, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) sRequest;
		HttpServletResponse response = (HttpServletResponse) sResponse;
		HttpSession session = request.getSession();
		// map 类型为Map<String,Object>
		Object map = session.getAttribute("CX_JSESSION_USER");
                System.out.println(map);
		// 第一次登录系统
		if (map == null) {
			// 获取单点登录服务器传递过来的用户信息 
			AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();
			if (principal != null) {
				Map<String, Object> attr = principal.getAttributes();
				if (attr != null) {
                                    //System.out.println("==============================================================="+attr);
					//String uuid = String.valueOf(attr.get("uuid"));
					//if(!StringUtils.isEmpty(uuid)) {
					session.setAttribute("CX_JSESSION_USER", attr);
                                        session.setAttribute("userInfo", JsonKit.toJson(attr)); 
                                        try{
                                        Map<String,String> result  = new Gson().fromJson(IntefaceOther.checkDoctorByGroup(StaticData.DOCTOR_MANAGER_GROUP,attr.get("uuid")+""), new TypeToken<Map<String,String>>(){}.getType());
					//System.out.println(result);
                                        if(result.get("data").equalsIgnoreCase("1")){
                                            //System.out.println("yes_______________");
                                            session.setAttribute("remote_user", "true");
                                        }
                                        }catch(Exception e){
                                        e.printStackTrace();}
				}
			}
		}
		
			chain.doFilter(sRequest, sResponse);
	}

	@Override
	public void init(FilterConfig config) throws ServletException {

	}

	@Override
	public void destroy() {

	}
}
